package com.jwgf.smarowindingmachine_unity.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import androidx.fragment.app.DialogFragment;

import com.jwgf.adapter.CommonAdapter;
import com.jwgf.adapter.ViewHolder;
import com.jwgf.common.ViewCache;
import com.jwgf.fault.FaultRecorder;
import com.jwgf.objectbox.HeadFaultRecord;
import com.jwgf.objectbox.HeadFaultRecord_;
import com.jwgf.objectbox.ObjectBox;
import com.jwgf.smarowindingmachine_unity.R;
import com.jwgf.smarowindingmachine_unity.logic.DatabaseChangedEvent.MachineFaultChanged;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import io.objectbox.Box;

public class MachineStateMachineLogFragment extends BaseFragment implements OnItemClickListener {
    private ListView listView;
    private CommonAdapter adapter;
    private List<HeadFaultRecord> list = new ArrayList<>();
    FaultRecorder faultRecorder = FaultRecorder.getInstance();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = ViewCache.getInstance().getView(R.layout.fragment_machine_state_machine_log);
        if (v == null) {
            v = inflater.inflate(R.layout.fragment_machine_state_machine_log, container, false);
        }

        listView = (ListView) v.findViewById(R.id.machine_record_list);
        listView.setAdapter(adapter = new CommonAdapter<HeadFaultRecord>(getActivity(), list,
                R.layout.item_fault_list) {
            @Override
            public void convert(final ViewHolder holder, final HeadFaultRecord item) {

                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
                Date timeDate = new Date(Long.valueOf(item.start));
                String datetimestr = formatter.format(timeDate);
                holder.setText(R.id.fault_time, datetimestr);
                holder.setText(R.id.fault_string, faultRecorder.getFault(item.faultCode));

            }
        });
        getData();
        listView.setOnItemClickListener(this);
        EventBus.getDefault().register(this);
        return v;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        int code = list.get(position).faultCode;
        DialogFragment dlg = new FaultDetailDialog();
        Bundle args = new Bundle();
        args.putInt(FaultDetailDialog.FAULT_CODE, code);
        dlg.setArguments(args);
        dlg.show(getFragmentManager(), "fault_detail");

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            EventBus.getDefault().unregister(this);
        } else {
            update(null);
            EventBus.getDefault().register(this);
        }
    }

    private void getData() {
        Box<HeadFaultRecord> box = ObjectBox.get().boxFor(HeadFaultRecord.class);
        final List<HeadFaultRecord> tempList = box.query().orderDesc(HeadFaultRecord_.start).build().find(0, 200);
        list.clear();
        list.addAll(tempList);
        adapter.notifyDataSetChanged();
    }

    @Subscribe
    public void update(MachineFaultChanged mfc) {
        getData();
    }

}
